package com.ctsi.service.impl;

import com.ctsi.common.OrgTypeEnum;
import com.ctsi.common.ResultVO;
import com.ctsi.entity.SysMenuOrg;
import com.ctsi.entity.req.AddSysUserMenuOrgReq;
import com.ctsi.manager.SysMenuOrgManager;
import com.ctsi.manager.SysMenuUserOrgManager;
import com.ctsi.service.SysMenuOrgService;
import com.ctsi.ssdc.admin.domain.CscpUser;
<<<<<<< HEAD
=======
import com.ctsi.ssdc.admin.domain.CscpUserDetail;
import com.ctsi.ssdc.admin.domain.dto.CscpUserRoleDTO;
import com.ctsi.ssdc.admin.repository.CscpUserDetailRepository;
import com.ctsi.ssdc.admin.repository.CscpUserRepository;
import com.ctsi.ssdc.admin.service.CscpUserRoleService;
import com.ctsi.ssdc.admin.service.CscpUserService;
>>>>>>> 68aa1d90d2566d16418fa4b72fc5cda36fdfbb57
import com.ctsi.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SysMenuOrgServiceImpl implements SysMenuOrgService {


    @Autowired
    private SysMenuOrgManager sysMenuOrgManager;

    @Autowired
    private SysMenuUserOrgManager sysMenuUserOrgManager;

    @Autowired
    private CscpUserService cscpUserService;

    @Autowired
    private CscpUserRepository cscpUserRepository;

    @Autowired
    private CscpUserDetailRepository cscpUserDetailRepository;

    @Autowired
    private CscpUserRoleService cscpUserRoleService;


    @Override
    public ResultVO<List<SysMenuOrg>> selectSysMenuByOrgId(Long id) {
        List<SysMenuOrg> sysMenuOrgs = sysMenuOrgManager.selectSysMenuByOrgId(id);
        if (Objects.isNull(sysMenuOrgs)) {
            log.error("未找到相关信息！");
            return ResultVO.error("未找到相关信息！");
        }
        return ResultVO.success(sysMenuOrgs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO insertSysMenuOrg(AddSysUserMenuOrgReq req) {

        //插入组织机构信息
        sysMenuOrgManager.insertSysMenuOrg(req);
        SysMenuOrg resp = sysMenuOrgManager.criteriaQuerySysMenu(req);
        log.info("SysMenuOrgServiceImpl.insertSysMenuOrg resp = {}", resp);
        req.setSysMenuOrgId(resp.getId());

        //如果是虚拟机构则无需创建相关用户
        if (OrgTypeEnum.ORG_TYPE_1.getCode().equals(req.getOrgType())) {
            return ResultVO.success();
        }

        //新建一个组织机构相关的用户
        if (cscpUserService.existByUsername(req.getUsername())) {
            log.error("用户名已存在！");
            return ResultVO.error("用户名已存在！");
        }
        CscpUser cscpUser = new CscpUser();
        cscpUser.setUsername(req.getUsername());
        cscpUser.setPassword(new BCryptPasswordEncoder().encode(req.getPassword()));
        cscpUserRepository.insert(cscpUser);

        CscpUser entity = cscpUserService.findByUsername(cscpUser.getUsername());
        log.info("SysMenuOrgServiceImpl.insertSysMenuOrg entity = {}", entity);
        req.setUserId(entity.getId().longValue());

        CscpUserDetail userDetail = new CscpUserDetail();
        userDetail.setUserId(entity.getId());
        userDetail.setRegisterTime(new Date());
        cscpUserDetailRepository.insert(userDetail);

        //建立组织用户关系
        sysMenuUserOrgManager.insertSysMenuUserOrg(req);

        //用户关联角色
        CscpUserRoleDTO cscpUserRoleDTO = new CscpUserRoleDTO();
        cscpUserRoleDTO.setUserId(req.getUserId().intValue());
        cscpUserRoleDTO.setRoleId(req.getRoleId());
        cscpUserRoleService.insert(cscpUserRoleDTO);

        return ResultVO.success();
    }

    @Override
    public ResultVO updateSysMenuOrgByOrgId(SysMenuOrg sysMenuOrg) {
        int resNo = sysMenuOrgManager.updateSysMenuOrgByOrgId(sysMenuOrg);
        if (resNo < 0) {
            log.error("更新数据失败！");
            return ResultVO.error("更新数据失败！");
        }
        return ResultVO.success();
    }

    @Override
    public ResultVO deleteSysMenuByOrgId(Long id) {
        int resNo = sysMenuOrgManager.deleteSysMenuByOrgId(id);
        if (resNo < 0) {
            log.error("删除数据失败！");
            return ResultVO.error("删除数据失败！");
        }
        return ResultVO.success();
    }

    @Override
    public ResultVO<List<SysMenuOrg>> getlist(SysMenuOrg sysMenuOrg) {
        List<SysMenuOrg> list = sysMenuOrgManager.list(sysMenuOrg);
        if (StringUtils.isArray(list)) {
            log.info("未找到相关信息！");
            return ResultVO.error("未找到相关信息！");
        }
        return ResultVO.success(list);
    }

    @Override
    public ResultVO<List<SysMenuOrg>> excludeChild(Long id) {
        List<SysMenuOrg> list = sysMenuOrgManager.list(new SysMenuOrg());
        Iterator<SysMenuOrg> iterator = list.iterator();
        while (iterator.hasNext()) {
            SysMenuOrg next = iterator.next();
            if (next.getId()==id) {
                iterator.remove();
            }
        }
        return ResultVO.success(list);
    }

    @Override
    public ResultVO<List<CscpUser>> selectUserlist(Long id) {
        return sysMenuOrgManager.selectUserList(id);
    }

    @Override
    public ResultVO<List<SysMenuOrg>> selectOrgList(Long id) {
        return sysMenuOrgManager.selectOrgList(id);
    }
}
